﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;
using System.Drawing;
using SD.Database;

namespace smallDeimos.Game
{
    public partial class Fleets : System.Web.UI.Page
    {
        int UserId;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
            if (UserFunctions.PlayerHasSpecialBulding(UserId, SD.Database.SpecialBuldings.OrbitalCC) == true)
            {
                if (UserFunctions.GetPlayerFleetsOnAir(UserId) >= 7)
                {
                    GridView2.Enabled = false;
                    Label1.Text = "You may not launch more fleets.";
                }
            }
            else
            {
                if (UserFunctions.GetPlayerFleetsOnAir(UserId) >= 5)
                {
                    GridView2.Enabled = false;
                    Label1.Text = "You may not launch more fleets.";
                }
            }
        }

        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int sId = 0;
            int pId = 0;
            GridViewRow row = GridView2.SelectedRow;
            if (row == null)
                return;
            try
            {
                sId = int.Parse(row.Cells[0].Text);
                pId = int.Parse(row.Cells[1].Text);
                Response.Redirect("FleetView.aspx?sId=" + sId + "&pId=" + pId);
            }
            catch (Exception ex)
            {
                if (row == null)
                    throw new Exception("Row null", ex);

                StringBuilder sb = new StringBuilder("_");
                foreach (TableCell c in row.Cells)
                    sb.Append(c.Text + "_");
                throw new Exception(sb.ToString(), ex);
            }
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string t = e.Row.Cells[1].Text;
                try
                {
                    int sId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "SystemId"));
                    int pId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "PlanetId"));
                    //string[] tmp = t.Split('#');
                    //int sId = int.Parse(e.Row.Cells[0].Text);
                    //int pId = int.Parse(tmp[1].Trim());
                    int planetOwner = SD.Database.Planets.PlanetOwner(sId, pId);
                   // Response.Write(planetOwner.ToString() + "." + UserId.ToString());
                    if (planetOwner != UserId)
                        e.Row.BackColor = Color.DarkRed;
                }
                catch (Exception ex)
                {
                    Response.Write(ex.ToString() + "<br/>");
                }
            }
        }
    }
}
